package com.example.listenmailforfsbot.config;

import com.example.listenmailforfsbot.util.ListenMail;
import com.example.listenmailforfsbot.util.SendFeiShuBot;
import com.example.listenmailforfsbot.util.TextFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author dftdla
 */
@Configuration
@RefreshScope
public class MailConfig {

    @Value("${listen.mail.host}")
    private String host;
    @Value("${listen.mail.port}")
    private Integer port;
    @Value("${listen.mail.usermail}")
    private String usermail;
    @Value("${listen.mail.password}")
    private String password;
    @Value("${listen.mail.protocol}")
    private String protocol;
    @Value("${listen.mail.folder}")
    private String folder;
    @Value("${listen.mail.filterTitle}")
    private String filterTitle;
    @Value("${listen.mail.skip}")
    private Boolean skip;

    @Value("${send.webHookUrl}")
    private String webHookUrl;
    @Value("${send.regexStr}")
    private String regexStr;

    @Value("${textFilter.regex}")
    private String textFilterRegex;
    @Value("#{${textFilter.filters}}")
    private Map<String,String> map;

    @Bean
    @RefreshScope
    public SendFeiShuBot sendFeiShuBot(){
        return new SendFeiShuBot(webHookUrl,regexStr);
    }

    @Bean
    @RefreshScope
    public ListenMail listenMail(){
        return new ListenMail(host, protocol, port, usermail, password, folder, filterTitle, skip);
    }

    @Bean
    @RefreshScope
    public TextFilter textFilter(){
        return new TextFilter(textFilterRegex,map);
    }

    @Override
    public String toString() {
        return "MailConfig{" + "\n" +
                "\thost = '" + host + '\'' + "\n" +
                "\tport = " + port + "\n" +
                "\tusermail = '" + usermail + '\'' + "\n" +
                "\tpassword.length = " + password.length() + "\n" +
                "\tprotocol = '" + protocol + '\'' + "\n" +
                "\tfolder = '" + folder + '\'' + "\n" +
                "\tfilterTitle = '" + filterTitle + '\'' + "\n" +
                "\tskip = " + skip + "\n" +
                "\twebHookUrl.length = " + webHookUrl.length() + "\n" +
                "\tregexStr = '" + regexStr + '\'' + "\n" +
                "\ttextFilterRegex='" + textFilterRegex + '\'' + "\n" +
                "\tfilterMap=" + map + "\n" +
                '}';
    }
}
